package physics.MovementControl;

import physics.GameObject;
import physics.Planet;

import common.Math3D;
import common.Point3D;
import common.Position3D;


public class PlanetMovementController extends AbstractMovementController {
	Planet planet;

	public PlanetMovementController(Planet planet){
		super(planet);
		this.hasMomentum = true;
		this.planet = planet;
	}

	@Override
	protected void doMoveMomentum() {
		super.doMoveMomentum();
		for(GameObject object : planet.getObjectsOnPlanet()){
			object.getPosition().getCenter().add(movementMomentum);
		}
	}



	@Override
	protected void updateMomentum() {
		super.updateMomentum();
		if (planet.getSun() != null){
			
			Point3D sunPosition = planet.getSun().getCenter();
			
			Point3D sunVector = Math3D.subPoint(planet.getCenter(),sunPosition); 
			double angle = 0.1 / sunVector.norm();
			sunVector.rotate(0, angle, 0);
			sunVector.add(sunPosition);
			
			movementMomentum = Math3D.subPoint(sunVector, planet.getCenter());
		}
	}

	@Override
	protected void doRotationMomentum() {
		super.doRotationMomentum();
		try{
		for(GameObject object : planet.getObjectsOnPlanet()){
			Position3D position = object.getPosition();
			position.getCenter().sub(planet.getPosition().getCenter());
			position.rotateWith(rotationMomentumLeft,rotationMomentumUp, rotationMomentumFace);
			position.getCenter().add(planet.getPosition().getCenter());
			//			 TODO : probably will need to also rotate object's movement controller momentum vector
		}
		}catch (Exception e){
		}
	}

}
